package bookstore;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class BookStore {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ArrayList list = new ArrayList();
        while (true){
        System.out.println("----欢迎来到kk书城----");
        System.out.println("1.展示书籍");
        System.out.println("2.上架书籍");
        System.out.println("3.下架书籍");
        System.out.println("4.退出书城");
         //显示主页面
Scanner sc = new Scanner(System.in);{

    int choice= sc.nextInt();

           if (choice==1) {
               try {
                   File f = new File("d:\\bookstore\\book.txt");
                   FileInputStream fi = new FileInputStream(f);
                   ObjectInputStream ois = new ObjectInputStream(fi);
                   list = (ArrayList) ois.readObject();
                   if (list.size() == 0) {
                       System.out.println("未上架书籍，请先上架书籍");
                       ois.close();
                   } else {
                       for (Object o : list) {
                           Book b = (Book) o;
                           System.out.println(b.getNum() + "-----" + b.getName() + "------" + b.getWriter());
                       }//遍历list集合，并挨个元素打印
                       ois.close();
                   }
                   }catch(FileNotFoundException exception){
                   System.out.println("未上架书籍，请先上架书籍");
               }
           }
            if (choice==2){
                System.out.println("请输入要上架书籍的编号");
                int num = sc.nextInt();
                System.out.println("请输入要上架书籍的名称");
                String name = sc.next();
                System.out.println("请输入要上架书籍的作者");
                String writer = sc.next();
                Book a = new Book(num,name,writer);
                //将输入的信息通过构造器初始化对象
                list.add(a);
                File f = new File("d:\\TestProject\\book.txt");
                FileOutputStream fos = new FileOutputStream(f);
                ObjectOutputStream OOS = new ObjectOutputStream(fos);
                OOS.writeObject(list);
                System.out.println("书籍上架成功");
                OOS.close();

            }
            if (choice==3){
                File f = new File("d:\\TestProject\\book.txt");
                FileInputStream fi = new FileInputStream(f);
                ObjectInputStream oi = new ObjectInputStream(fi);
                list = (ArrayList)oi.readObject();
                System.out.println("请输入要删除的书籍的编号");//提示用户输入删除书籍的编号
                int num = sc.nextInt();
               // book.remove(num-1);
                for (int i = 0; i < list.size();i++){
                    Book bdel = (Book)list.get(i);
                    if (num==bdel.getNum()){
                        list.remove(bdel);
                        oi.close();
                        FileOutputStream fos = new FileOutputStream(f);
                        ObjectOutputStream OOS = new ObjectOutputStream(fos);
                        OOS.writeObject(list);
                        OOS.close();
                        break;
                    }
                }
                System.out.println("书籍移除成功");

            }
            if (choice==4){
                System.out.println("已退出书城");
                break;
            }

            }
        }

    }
}
